#pragma once

#include "base_types.h"
#include "poller.h"
#include "timer.h"
#include <thread>
#include <atomic>
#include <functional>

// Reactor class - main event loop
class CCReactor {
public:
    CCReactor();
    ~CCReactor();
    
    // Initialize reactor
    int Init();
    
    // Start reactor event loop
    int Start();
    
    // Stop reactor
    int Stop();
    
    // Add object to reactor
    int AddObject(CPollerObject* obj);
    
    // Remove object from reactor
    int RemoveObject(CPollerObject* obj);
    
    // Check if reactor is running
    bool IsRunning() const { return running; }
    
    // Set event loop callback
    void SetEventCallback(std::function<void()> callback) { event_callback = callback; }
    
    // Get reactor instance
    static CCReactor& Instance();
    
private:
    // Event loop
    void EventLoop();
    
    // Process timers
    void ProcessTimers();
    
    // Handle signals
    void SetupSignalHandlers();
    
private:
    CPoller poller;
    std::thread event_thread;
    std::atomic<bool> running;
    std::atomic<bool> stop_requested;
    std::function<void()> event_callback;
    
    // Timer processing
    int timer_interval_ms;
};

// Global reactor instance
extern CCReactor* g_reactor;